import { reqGetCode, reqUserRegister,reqUserLogin,reqUserInfo } from "@/api";
//登录注册组件的仓库
const state = {
    code:"",
    token: localStorage.getItem('TOKEN'),
    userInfo:{}
};
const mutations = {
    GETCODE(state, code) {
        state.code = code;
    },
    USERLOGIN(state,token) {
        state.token = token;
    },
    GETUSERINFO(state,userInfo){
        state.userInfo = userInfo;
    }
};
const actions = {
    //获取验证码
    async getCode({ commit }, phone) {
        let result = await reqGetCode(phone);
        if (result.code == 200) {
            commit("GETCODE", result.data);
            return 'ok'
        } else {
            return Promise.reject(new Error('res.message'));
        }
    },
    //用户注册
    async userRegister({commit}, user) {
        let result = await reqUserRegister(user);
        if(result.code==200){
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //登陆业务[token]
    async userLogin({commit},data){
        let result = await reqUserLogin(data);
        if(result.code==200){
            commit('USERLOGIN',result.data.token);
            //持久化存储token
            localStorage.setItem("TOKEN",result.data.token);
            return 'ok'
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //获取用户信息
    async getUserInfo({commit}){
        let result = await reqUserInfo();
        if(result.code==200){
            commit('GETUSERINFO',result.data);
        }
    }
};
const getters = {};
export default {
    state,
    mutations,
    actions,
    getters
}